package cn.ybk.pet.controller;

import cn.ybk.basic.jwt.UserInfo;
import cn.ybk.basic.util.LoginContext;
import cn.ybk.pet.dto.AcceptDto;
import cn.ybk.pet.dto.PublishDto;
import cn.ybk.pet.service.ISearchMasterMsgService;
import cn.ybk.pet.domain.SearchMasterMsg;
import cn.ybk.pet.query.SearchMasterMsgQuery;
import cn.ybk.basic.util.AjaxResult;
import cn.ybk.basic.util.PageList;
import cn.ybk.user.domain.Logininfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 发布宠物寻主
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody PublishDto dto) {
        searchMasterMsgService.publish(dto);
        return AjaxResult.me().success();
    }

    /**
     * 待处理 State 为 1
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();
        query.setShopId(userInfo.getShop().getId());
        query.setState(1);
        return searchMasterMsgService.queryByPage(query);


    }

    /**
     * 寻主池 state = 1
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();
        query.setShopId(null);
        query.setState(1);
        return searchMasterMsgService.queryByPage(query);
    }


    /**
     * 接单 state = 3
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto) {
        searchMasterMsgService.accept(dto);
        return AjaxResult.me().success();
    }

    /**
     * 拒单 state = 1 审核通过
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        searchMasterMsgService.reject(id);
        return AjaxResult.me().success();
    }

    /**
     * 完成 state = 4 审核通过
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();
        query.setShopId(userInfo.getShop().getId());
        query.setState(4);
        return searchMasterMsgService.queryByPage(query);
    }

}
